package com.ljy.my_study.lintcode.最大乘积子数组;
/** 
* @author James
* @date 2019年5月9日 
*/
public class TestMain {
//	Find the contiguous subarray within an array (containing at least one number) which has the largest product.
//	Have you met this question in a real interview?  Yes
//	Problem Correction
//	Example
//	Example 1:
//
//	Input:[2,3,-2,4]
//	Output:6
//	Example 2:
//
//	Input:[-1,2,4,1]
//	Output:8
	public static void main(String[] args) {
		int[] nums= {-1};
		System.out.println(maxProduct(nums));
	}
	
	public static int maxProduct(int[] nums) {
		int len=nums.length;
		if(len<=0) {
			return 0;
		}
		int max=nums[0],min=nums[0],res=nums[0];
		int maxPre=nums[0],minPre=nums[0];
		for(int i=0;i<len;i++) {
			max=Math.max(nums[i], Math.max(maxPre*nums[i], minPre*nums[i]));
			min=Math.min(nums[i], Math.min(maxPre*nums[i], minPre*nums[i]));
			res=Math.max(max, min);
			maxPre=max;
			minPre=min;
		}
		return res;
	}
}
